/**
 * 功能描述�?
 */

define(function (require, exports, module) {
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	
    module.exports = {
            saveAction : function(widget){
            	if(!widget.get("form").valid()){
            		return false;
            	}
            	var formData = widget.get("form").getData();
            	if(!formData.perNumber){
            		formData.perNumber = formData.signUpNumber;
            	}
            	var signUpRooms = [];
            	if(widget.get("type") != "NonMem"){
            		var signUpRoom = {
                			roomType : formData.roomType,
                			count : formData.count,
                	};
            		signUpRooms.push(signUpRoom);
            	}
            	
            	var perDatas = widget.get("pergrid").getData();//pereditgrid
            	var savePerDatas = [];
            	for(var i in perDatas){
            		var savePerData = {
            				signUpRegister : perDatas[i].signUpRegister.pkSignUpRegister
            		}
            		if(perDatas[i].pkNonMemberRegister){
            			savePerData["version"] = perDatas[i].version;
            			savePerData["pkNonMemberRegister"] = perDatas[i].pkNonMemberRegister
            		}else{
            			savePerData["version"] = 0;
            		}
            		savePerDatas.push(savePerData);
            	}
            	if(savePerDatas.length == 0){
            		Dialog.alert({
            			tip : "提示",
            			content : "请添加体验人数！"
            		});
            		return false;
            	}
            	var data = {
            			nonMemberGroup : formData,
            			signUpRooms : signUpRooms,
            			signUps : savePerDatas
            	};
            	aw.saveOrUpdate("api/nonmembergroup/saveall", aw.customParam(data), function(datas){
            		widget.show([".J-grid"]).hide([".J-form",".J-pergrid"]);
            		if(widget.get("type") == "NonMember"){
            			widget.get("subnav").hide(["save", "return"]).show(["search","servicePoint", "add","status", "startDate"]);
            		}else{
            			widget.get("subnav").hide(["save", "return"]).show(["search","add","status", "operateDate"]);
            		}
            		widget.get("grid").refresh();
            	})
            },
            
            saveEditgrid : function(widget){

            	if(!widget.get("form").valid()){
            		return false;
            	}
            	var formData = widget.get("form").getData();
            	
            	var perDatas = widget.get("pereditgrid").getData();
            	if(perDatas.length == 0){
            		Dialog.alert({
            			tip : "提示",
            			content : "请添加房型！"
            		});
            		return false;
            	}
            	for ( var i in perDatas) {
            		perDatas[i].roomType=perDatas[i].roomType.pkRoomType;
				}
            	var data = {
            			nonMemberGroup : formData,
            			signUpRooms : perDatas,
            	};
            	aw.saveOrUpdate("api/nonmembergroup/saveadd", aw.customParam(data), function(datas){
            		widget.show([".J-grid"]).hide([".J-form",".J-pergrid",".J-pereditgrid"]);
            		if(widget.get("type") == "NonMember"){
            			widget.get("subnav").hide(["save", "return"]).show(["servicePoint", "add","status", "startDate"]);
            		}else{
            			widget.get("subnav").hide(["save", "return"]).show(["add","status", "operateDate"]);
            		}
            		widget.get("grid").refresh();
            	})
            
            }
            
        };
});
